package wili.sql;

import java.util.ArrayList;
import java.util.List;

import wili.c.Ca;

/**
 * @author weila 2022年7月23日
 */
public class Col implements Comparable<Col> {
	public String tname;

	public String name;

	public boolean isPrimKey;

	public Col pCol;

	public List<Col> fColList;
	SqlCont cont;

	Tab table;

	public String toString() {
		String st = "";
		if (table.pKeys != null && table.pKeys.contains(this)) {
			st += "P ";
		} else {
			st += "- ";
		}
		if (table.iKeys != null && table.iKeys.contains(this)) {
			st += "I ";
		} else {
			st += "- ";
		}
		if (table.eKeys != null && table.eKeys.contains(this)) {
			st += "F ";
		} else {
			st += "- ";
		}
		st += name;
		return st;
	}

	public Col(String name, Tab table) {
		this.name = name;
		this.table = table;
	}

	public void addForeignKey(Col col) {
		if (fColList == null)
			fColList = new ArrayList<>();
		if (fColList.contains(col))
			Ca.asert(false, "");
		if (fColList.size() > 50)
			Ca.log(1, this.table.name + "." + name + " has tooMuch foreignKey: " + fColList.size());
		this.fColList.add(col);
	}

	public void setPrimaryColumn(Col pCol) {
		this.pCol = pCol;
		pCol.addForeignKey(this);
	}

	public void setIsPrimKey() {
		this.isPrimKey = true;
	}

	@Override
	public int compareTo(Col o) {
		return this.name.compareTo(o.name);
	}
}
